package com.fzd.code.captcha.service;

import com.fzd.code.captcha.service.i.ICaptchaManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class RedisCaptchaManager implements ICaptchaManager {

    private RedisTemplate<String, String> redis;

    @Autowired
    private Constants constants;

    @Autowired
    public void setRedis(StringRedisTemplate redis) {
        this.redis = redis;
        redis.setKeySerializer(new StringRedisSerializer());
    }

    @Override
    public void set(String id,String captcha) {
        //存储到redis并设置过期时间
        redis.boundValueOps(id+"-"+key).set(captcha, constants.getExpireHours(), TimeUnit.HOURS);
    }

    @Override
    public String get(String id) {
        return redis.boundValueOps(id+"-"+key).get();
    }

    @Override
    public void delete(String id) {
        redis.delete(id);
    }
}
